package springtive.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class FileUtil {
	
	public static String concatePath(String path1, String path2) {
		if (StringUtil.isEmpty(path1)) return path2;
		if (StringUtil.isEmpty(path2)) return path1;
		
		path1 = path1.replace("\\\\", "/");
		path2 = path2.replace("\\\\", "/");

		return (path1 + "/" +path2).replaceAll("[/]+", "/");
	}
	
	public static List<String> getFileList(final String path) throws IOException {
		List<String> fileList = new ArrayList<String>();
		File p = new File(path);
		if (!p.exists())
			return null;
		
		traverse(fileList, p);
		
		return fileList;
	}
	
	public static void traverse(List<String> list, final File f) throws IOException {
		if (f.isDirectory()) {
			final File[] childs = f.listFiles();
			for (File child : childs)
				traverse(list, child);
		}
		else
			list.add(f.getPath());
	}
	
	public static boolean makeDir(String path) {
		boolean isSuccess = false;
		File file = new File(path);
		if(!file.exists()) {
			isSuccess = file.mkdirs();
		}
		return isSuccess;
	}
	
	public static boolean write(String filename, String contents) {
		File file = new File(filename);
		BufferedWriter out = null;
		try {
			makeDir(getPath(filename));
			out = new BufferedWriter(new FileWriter(file));
			out.write(StringUtil.nvl(contents));
		} 
		catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		finally {
			if (out != null) try { out.close(); } catch (IOException e) {}
		}
		
		return file.exists();
	}
	
	public static boolean write(String filename, byte[] contents) {
		File file = new File(filename);
		FileOutputStream out = null;
		try {
			makeDir(getPath(filename));
			out = new FileOutputStream(file);
			out.write(contents);
		}
		catch (IOException e) {
			e.printStackTrace();
			return false;
		} 
		finally {
			if (out != null) try { out.close(); } catch (IOException e) {}
		}
		
		return file.exists();
	}
	
	public static String getFileExtend(String filename) {
		String[] tmpArray = StringUtil.split(filename, ".");

		if (tmpArray == null || tmpArray.length < 1)
			return filename;
		else
			return tmpArray[tmpArray.length - 1];
	}

	public static String getFileName(String filename) {
		if (filename == null || filename.equals(""))
			return "";
		
		int index = filename.lastIndexOf('/');
		if (index != -1)
			return  filename.substring(index + 1);
		else
			return filename;
	}
	
	public static String getPath(String filename) {
		if (filename == null || filename.equals(""))
			return "";
		
		int index = filename.lastIndexOf('/');
		if (index != -1)
			return  filename.substring(0, index);
		else
			return filename;
	}
	
	public final static boolean copy(File fromFile, File toFile) {
		FileInputStream in = null;
		FileOutputStream out = null;
		BufferedInputStream inBuffer = null;
		BufferedOutputStream outBuffer = null;
		
		try{
			in = new FileInputStream(fromFile);
			out = new FileOutputStream(toFile);
			inBuffer = new BufferedInputStream(in);
			outBuffer = new BufferedOutputStream(out);

			int theByte = 0;
			while ((theByte = inBuffer.read()) > -1){
				outBuffer.write(theByte);
			}
			outBuffer.close();
			inBuffer.close();
			out.close();
			in.close();


			if (fromFile.length() != toFile.length()){
				toFile.delete();
				return false;
			}
			return true;
		}
		catch (IOException e){
			e.printStackTrace();
			return false;
		}
		finally {
			if (outBuffer != null) try { outBuffer.close(); } catch (IOException e) {}
			if (inBuffer != null) try { inBuffer.close(); } catch (IOException e) {}
			if (out != null) try { out.close(); } catch (IOException e) {}
			if (in != null) try { in.close(); } catch (IOException e) {}
		}
	}
	
	public static List<String> readToList(String filename) {
		BufferedReader br = null;
		
		try {
			List<String> lines = new ArrayList<String>();
			br = new BufferedReader(new FileReader(filename));
			String line = null;
			while ((line = br.readLine()) != null) {
				lines.add(line);
			}
			
			return lines;
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
			
		} 
		finally {
			if (br != null) try { br.close(); } catch (IOException e) {}
		}
	}
	
	public static String read(String filename) {
		BufferedReader br = null;
		
		try {
			StringBuffer sb = new StringBuffer();
			br = new BufferedReader(new FileReader(filename));
			String line = null;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
			
		} 
		finally {
			if (br != null) try { br.close(); } catch (IOException e) {}
		}
	}

	public final static boolean move (File fromFile, File toFile){
		if(fromFile.renameTo(toFile)){
			return true;
		}

		if (copy(fromFile, toFile)){
			return fromFile.delete();
		}
		return false;
	}
	
	public static void deleteFiles(File path) {
		deleteFiles(path, false);
	}
	
	public static void deleteFiles(File path, boolean isRecursive) {
		File[] childFile = path.listFiles();
		
		if (childFile == null)
			return;
		
		for (File aFile : childFile) {
			if (aFile.isFile())
				aFile.delete();
            else
            	deleteFiles(aFile, isRecursive);
		}
	}
	
	public static boolean deleteDirs(File path) {
		if (!path.exists()) return false;
		
		File[] childFile = path.listFiles();
		
		if (childFile == null)
			return false;
		
		for (File aFile : childFile) {
			if (aFile.isDirectory())
				deleteDirs(aFile);
            else
            	aFile.delete();
		}
		
		return path.delete();
	}
}
